package com.example.demo.modules.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.util.ResultUtil;
import com.example.demo.common.util.entity.ListSub;
import com.example.demo.common.util.entity.PageFactory;
import com.example.demo.common.util.entity.Result;
import com.example.demo.common.util.entity.ResultEnum;
import com.example.demo.modules.sys.dto.dict.QueryDTO;
import com.example.demo.modules.sys.dto.dict.SaveOrUpdateDTO;
import com.example.demo.modules.sys.entity.Dict;
import com.example.demo.modules.sys.service.IDictService;
import com.example.demo.modules.test.entity.Class;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典 前端控制器
 * </p>
 *
 * @author lmy
 * @since 2023-01-29
 */
@RestController
@RequestMapping("/sys/dict")
@Api(tags = "系统模块")
public class DictController {

    @Resource
    private IDictService iDictService;

    @GetMapping("/page")
    @ApiOperation(value = "字典分页列表", notes = "字典分页列表")
    public Result<ListSub<Dict>> page(PageFactory pageFactory, QueryDTO queryDTO){
        Page<Dict> page = new Page<>(pageFactory.getStart(), pageFactory.getLength());
        IPage<Dict> iPage = iDictService.listDict(page, queryDTO);
        return ResultUtil.success(iPage);
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增字典项", notes = "新增字典项(前端传父id,type(选传),name,value,sort)")
    public Result save(@RequestBody SaveOrUpdateDTO saveOrUpdateDTO){
        ResultEnum result = iDictService.saveDict(saveOrUpdateDTO);
        return ResultUtil.info(result);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新字典项", notes = "更新字典项")
    public Result update(@RequestBody SaveOrUpdateDTO saveOrUpdateDTO){
        ResultEnum result = iDictService.updateDict(saveOrUpdateDTO);
        return ResultUtil.info(result);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除字典项", notes = "删除字典项")
    public Result delete(@PathVariable("id") String id){
        ResultEnum result = iDictService.deleteDict(id);
        return ResultUtil.info(result);
    }

}

